package org.ziegler.hotfix;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class GenFixConfig {

    private static final String CONFIG_PATH = "F:\\ForsakenWorld\\cn20201113\\trunk\\public\\config\\";

    public static void main(String[] args) throws IOException, InterruptedException {

        // TODO code 解析文件名称
        final Scanner scanner = new Scanner(System.in);

        System.out.println("请输入...");

        Set<String> fileNameList = new HashSet<>();
        while (true) {
            final String s = scanner.nextLine();
            if (s.length() == 0) {
                break;
            }

            fileNameList.add(s);
        }

        final GenFixConfig genFixConfig = new GenFixConfig();
        genFixConfig.deleteFiles("./");

        for (String fileName : fileNameList) {
            genFixConfig.test2(fileName.trim());
        }


//        genFixConfig.test2("Item.xlsx");
//        genFixConfig.test2("ActivityConfiguration.xlsx");

//        genFixConfig.test2(new String[]{"Item.xlsx", "ActivityConfiguration.xlsx"});
    }

    public void test1() throws IOException, InterruptedException {
        // 显示cmd窗口
        String clearFixConfigBat = "cmd.exe /c 移除所有自动生成的文件.bat";
//        String clearFixConfigBat = "cmd.exe /c 移除所有自动生成的文件.bat";
        final boolean bOK = executeCmd(clearFixConfigBat);
        System.out.println("bOK = " + bOK);


    }

    public void test2(String fileName) throws IOException, InterruptedException {
        // 显示cmd窗口
        String param = CONFIG_PATH + fileName;
//        String clearFixConfigBat = "cmd.exe /c 生成fixconfig里使用的json-把配置表拖上来.bat " + param + " c";
        String clearFixConfigBat = "cmd.exe /c start 生成fixconfig里使用的json-把配置表拖上来.bat " + param + " c";
        final boolean bOK = executeCmd(clearFixConfigBat);
        System.out.println("bOK = " + bOK);
    }

    boolean executeCmd(String batFile) throws IOException, InterruptedException {
        final Runtime runtime = Runtime.getRuntime();
        final Process processResult = runtime.exec(batFile);
        // 等待cmd执行完成
        processResult.waitFor();
        final int exitValue = processResult.exitValue();
        processResult.destroy();
        return exitValue == 0;
    }

    public void test2(String[] fileName) throws IOException, InterruptedException {
        // 显示cmd窗口

//        String clearFixConfigBat = "cmd.exe /c 生成fixconfig里使用的json-把配置表拖上来.bat " + param + " c";
        String[] cmdArray = new String[fileName.length];

        for (int i = 0; i < fileName.length; i++) {
            String param = "C:\\MyWorkPlace\\cn20201015\\trunk\\public\\config\\" + fileName[i];
            cmdArray[i] = "cmd.exe /c start 生成fixconfig里使用的json-把配置表拖上来.bat " + param + " c";
        }

        final boolean bOK = executeCmd(cmdArray);
        System.out.println("bOK = " + bOK);
    }

    boolean executeCmd(String[] batFile) throws IOException, InterruptedException {
        final Runtime runtime = Runtime.getRuntime();
        final Process processResult = runtime.exec(batFile);
        // 等待cmd执行完成
        processResult.waitFor();
        final int exitValue = processResult.exitValue();
        processResult.destroy();
        return exitValue == 0;
    }

    public void deleteFiles(String path) {
        final File filePath = new File(path);
        for (File file : filePath.listFiles()) {
            if (file.getName().contains(".fixconfig.txt")) {
                file.delete();
                System.out.println("delete file = " + file.getName());
            }
        }
    }
}
